package org.swingDemo;

import javax.swing.text.MaskFormatter;
import java.text.ParseException;

public class MaskFormatDemo {

    public static MaskFormatter createFormatter(String s) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(s);
        } catch (ParseException ex) {
            System.err.println("formatter is bad: " + ex.getMessage());
            System.exit(-1);
        }
        return formatter;
    }

    public static void main(String[] args) {
        try {
            String value = "123";
            String[] formatterParam = {"#", "##", "###", "####"};
            for (String param : formatterParam) {
                MaskFormatter formatter = createFormatter(param);
                System.out.println(param + " << " + formatter.valueToString(value));
            }

            value ="abc";
            String[] formatterParam1 = {"UUUU", "LLL"};
            for (String param : formatterParam1) {
                MaskFormatter formatter = createFormatter(param);
                System.out.println(param + " << " + formatter.valueToString(value));
            }

        } catch (ParseException ex) {
            System.err.println("formatter is bad: " + ex.getMessage());
            System.exit(-1);
        }
    }
}
